package com.syedu.day15;

/**
 * description：Java多线程死锁
 * 交叉申请锁标记,导致锁无法释放,程序陷入卡死的情况
 * Java一旦发生死锁,无法解决,所以只能通过编码谨慎来避免
 *
 * @author Administrator
 * @date 2022/4/1 16:19
 */
public class DeadLock {
    //静态属性,所以o1和o2在当前程序中有且只有一份
    //public static Object o1 = new Object();
    //public static Object o2 = new Object();

    //改用普通属性
    public Object o1 = new Object();
    public Object o2 = new Object();

    public static void main(String[] args) {
        Thread t1 = new T1();
        Thread t2 = new T2();
        t1.start();
        t2.start();
    }
}

class T1 extends Thread {
    @Override
    public void run() {
        synchronized (new DeadLock().o1) {
            System.out.println("T1的外");
            synchronized (new DeadLock().o2) {
                System.out.println("T1的内");
            }
        }
    }
}

class T2 extends Thread {
    @Override
    public void run() {
        synchronized (new DeadLock().o2) {
            System.out.println("T2的外");
            synchronized (new DeadLock().o1) {
                System.out.println("T2的内");
            }
        }
    }
}